package TrackModel;
import java.util.ArrayList;

import TrackController.TrackController;
import Utilities.Message;

/**
 * @author Jordan
 *Sub-Class of Block that adds additional functionality for a Block that houses a Track Controller
 */
public class ControllerBlock extends Block 
{
	TrackController tc;
	ArrayList<Block> blocksInRange; // ArrayList of Block objects that this Track Controller controls
	String[] ranges;
	int[][] blockRanges; // The ranges of Block IDs that this Block Controller controls. Used to create the ArrayList of Blocks.
	int id;
	Message theMessage; // Message objects begin on Track Controller Blocks and are passed from there
	
	/**Constructor for a Controller Block object. Sets the additional attributes.
	 * @param parts - The String array containing the attributes that need to be set for this Controller Block
	 */
	public ControllerBlock(String[] parts)
	{
		super(parts);
		id = Integer.parseInt(parts[12]);
		blocksInRange = new ArrayList<Block>();
		ranges = parts[13].split(":");
		blockRanges = new int[ranges.length][2];
		
		for (int i = 0; i < ranges.length; i++)
		{
			String[] range = ranges[i].split(",");
			blockRanges[i][0] = Integer.parseInt(range[0]);
			blockRanges[i][1] = Integer.parseInt(range[1]);
		}
	}
	
	/**
	 * @return Returns an integer array of block IDs that represent the range of Blocks that the Track Controller controls
	 */
	public int[][] getBlockRanges()
	{
		return blockRanges;
	}
	
	/**
	 * @param theBlock - The Block object to be added to the ArrayList of Blocks that this Track Controller controls
	 */
	public void createBlocksInRange(Block theBlock)
	{
		blocksInRange.add(theBlock);
	}
	
	/**
	 * @return Returns the ArrayList of Blocks that this Track Controller controls
	 */
	public ArrayList<Block> getControllerBlocks()
	{
		return blocksInRange;
	}
	
	/**
	 * Creates a Track Controller object once everything else has been initialized
	 */
	public void initializeTrackController()
	{
		tc = new TrackController(id, blocksInRange);
	}
	
	/**
	 * @return Returns true if the Track Controller has a message to send out onto the track, otherwise returns false
	 */
	public boolean hasMessage()
	{	
		theMessage = tc.getNextMessage();
		if (theMessage != null)
			return true;
		else
			return false;
						
	}
	
	/**
	 * @return Returns the Message that is on the Track Controller so that it can be sent out along the track
	 */
	public Message getMessage()
	{
		return theMessage;
	}
	
	
}
